package com.jams.goodox.activity.mine.address;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hangzhou.jin.customview.LoadView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.adapter.MineAddressAdapter;
import com.jams.goodox.entity.Address;
import com.jams.goodox.entity.MyServiceAddress;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.HomeService;
import com.jams.goodox.utils.StyleUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by Jin on 2016/5/14.
 * Description 我的服务地址
 */
public class MineAddressActivity extends BaseActivity {

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.loadView)
    LoadView loadView;

    private List<Address> curList;
    private MineAddressAdapter mAdapter;
    public static final String NeedResult = "NeedResult";
    private boolean needResult;//上一个页面是否需要回调,默认需要

    @Override
    protected int getLayoutId() {
        return R.layout.mine_address;
    }

    @Override
    protected void init() {
        //获取从哪个页面过来的
        boolean isComeFromMineFragment = getIntent().getBooleanExtra("isComeFromMineFragment", false);
        //从我的页面过来，是进入“我的地址页面”，其余是进入“收货地址”
        StyleUtil.titleBackKeyWithListener(this, isComeFromMineFragment ? "我的地址" : "收货地址", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        //获取是否需要带回返回值
        needResult = getIntent().getBooleanExtra(NeedResult, true);
        curList = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        mAdapter = new MineAddressAdapter(curList, isComeFromMineFragment, true);
        recyclerView.setAdapter(mAdapter);

        mAdapter.setOnRecyclerViewItemChildClickListener(new BaseQuickAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, final int i) {
                switch (view.getId()) {
                    case R.id.rl1:
                        if (needResult) {
                            Intent i2 = new Intent();
                            Bundle bb = new Bundle();
                            bb.putSerializable("bean", curList.get(i));
                            i2.putExtras(bb);
                            setResult(RESULT_OK, i2);
                            finish();
                        }
                        break;
                    case R.id.mine_address_edit_btn:
                        Bundle b = new Bundle();
                        b.putSerializable("bean", curList.get(i));
                        qStartActivity(MineAddressEditActivity.class, b);
                        break;
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }

    private void loadData() {
        enqueue(createCallService(HomeService.class).addressList(SePref.getToken()), new RetrofitCallbackListener<MyServiceAddress>() {
            @Override
            public void onSuccess(Call<MyServiceAddress> call, MyServiceAddress result) {
                curList = result.getData();
                mAdapter.setNewData(curList);

                if (null != curList && curList.size() == 0) {
                    loadView.loadCompleteNoData(R.mipmap.holder_no_address, R.string.no_data_text_address);
                    return;
                }

                loadView.loadComplete();
            }

            @Override
            public void onError(String msg) {
                loadView.loadComplete();
            }
        });
    }

    @OnClick(R.id.mine_address_add)
    public void onClick() {
        qStartActivity(MineAddressAddActivity.class);
    }

    @Override
    public void onBackPressed() {
        if(curList.size()==0){
            finish();
            return;
        }
        Intent i2 = new Intent();
        Bundle bb = new Bundle();
        bb.putSerializable("bean",curList.get(0));
        i2.putExtras(bb);
        setResult(RESULT_OK, i2);
        finish();
    }
}

